package org.spiderflow.core.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.spiderflow.core.dto.UserDto;
import org.spiderflow.core.mapper.UserMapper;
import org.spiderflow.core.model.User;
import org.spiderflow.core.utils.EncodeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.security.auth.login.LoginContext;

/**
 * @author fanyushuai
 * @version 1.0
 * @description: TODO
 * @date 2021/12/6 18:07
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    private UserMapper userMapper;

    public boolean login(UserDto userDto){
        boolean flag = false;
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getName,userDto.getName()).eq(User::getPassword,EncodeUtils.des(userDto.getPassword()));
        User user = userMapper.selectOne(userLambdaQueryWrapper);
        if(null != user){
            flag = true;
            StpUtil.login(user.getId());
        }
        return flag;
    }
}
